package com.danger.Activity.Account;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.danger.Activity.R;
import com.danger.MyApplication;
import com.danger.Util.FileCacheService;

import java.util.HashMap;
import java.util.Map;

public class ThemeActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar theme_toolbar;
    private RelativeLayout theme_scroll_linear;
    private RelativeLayout theme_another_linear;
    private ImageView theme1_imageView;
    private ImageView theme2_imageView;
    private TextView toolbar_title;
    private Map<String, Object> map = new HashMap<String, Object>();
    private FileCacheService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_theme);

        initView();
        initEvent();
    }

    private void initView() {
        theme_toolbar = (Toolbar) findViewById(R.id.theme_toolbar);
        theme_toolbar.setTitle("");
        toolbar_title = (TextView) findViewById(R.id.toolbar_title);
        toolbar_title.setText("主题");
        setSupportActionBar(theme_toolbar);
        service = new FileCacheService(this);
        theme_scroll_linear = (RelativeLayout) findViewById(R.id.theme_scroll_linear);
        theme_another_linear = (RelativeLayout) findViewById(R.id.theme_another_linear);
        theme1_imageView = (ImageView) findViewById(R.id.theme1_imageView);
        theme2_imageView = (ImageView) findViewById(R.id.theme2_imageView);
        if (service.getSharePreference("setting").get("theme").equals("1")) {
            theme1_imageView.setVisibility(View.VISIBLE);
            theme2_imageView.setVisibility(View.GONE);
        } else {
            theme2_imageView.setVisibility(View.VISIBLE);
            theme1_imageView.setVisibility(View.GONE);
        }
    }

    private void initEvent() {
        theme_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        theme_scroll_linear.setOnClickListener(this);
        theme_another_linear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.theme_scroll_linear:
                theme1_imageView.setVisibility(View.VISIBLE);
                theme2_imageView.setVisibility(View.GONE);
                map.put("theme", "1");
                break;
            case R.id.theme_another_linear:
                theme2_imageView.setVisibility(View.VISIBLE);
                theme1_imageView.setVisibility(View.GONE);
                map.put("theme", "2");
                break;
        }
        map.put("audio_subtitle", service.getSharePreference("setting").get("audio_type"));
        map.put("audio_type", service.getSharePreference("setting").get("audio_type"));
        map.put("audio_rate", service.getSharePreference("setting").get("audio_rate"));
        if (service.saveSharePreference("setting", map)) {
            Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "设置失败", Toast.LENGTH_SHORT).show();
        }
    }
}
